package controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import model.Admin;
import model.Product;
import model.User;
import service.IAdminService;
import service.IProductService;
import service.impl.AdminServiceImpl;
import service.impl.ProductServiceImpl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class IndexController extends Controller {
    public void index() {
        String pageStr = getPara("page");
        String rowsStr = getPara("rows");
        int page = pageStr!=null?Integer.parseInt(pageStr):1;
        int rows = rowsStr!=null?Integer.parseInt(rowsStr):6;

        IProductService productService = new ProductServiceImpl();

        String query = getPara("query");
        try {
            if(query!=null) {
                query = URLDecoder.decode(query, "UTF-8");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if(query!=null&&!"".equals(query)){
            Page<Product> products = productService.searchProductsByQuery(page,rows,query,"id");
            setAttr("products", products);
            setAttr("query",query);
        }else {
            Page<Product> products = productService.searchProducts(page, rows, "id");
            setAttr("products", products);
        }

        render("/front/index.jsp");
    }


}